package com.zw.user.controller;/*
 *@program:b2c-cloud-store
 *@author:
 *@Time: 2022/11/21  20:11
 *@description: 用户地址管理接口
 */

import com.zw.param.AddressListParam;
import com.zw.param.AddressRemoveParam;
import com.zw.pojo.Address;
import com.zw.user.service.UserAddressService;
import com.zw.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 用户地址查询接口
     * @param addressListParam 接收用户地址参数实体对象，内有注解校验（jsr 303）
     * @param result 获取校验结果
     * @return 结果 001 004
     */
    @PostMapping("/list")
    public R list(@RequestBody @Validated AddressListParam addressListParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，查询失败");
        }
        return userAddressService.list(addressListParam.getUserId());
    }

    /**
     * 用户地址保存接口，返回最新地址信息
     * @param address 接收参数用户地址参数实体类，内有注解进行参数校验
     * @param result 获取校验结果
     * @return 结果 001  004  list<address>
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated Address address,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，不可保存");
        }
        return userAddressService.save(address);
    }


    /**
     * 用户地址删除接口
     * @param addressRemoveParam 接收地址id实体对象，使用jsr 303注解校验
     * @param result 获取校验后的记过
     * @return 结果 001 003
     */
    @PostMapping("/remove")
    public R remove(@RequestBody @Validated AddressRemoveParam addressRemoveParam,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，不可删除");
        }
        return userAddressService.remove(addressRemoveParam);
    }
}
